package com.aishua.fire.jpa;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.aishua.fire.jpa.entity.Perm;

public interface PermDao extends JpaRepository<Perm, Long>,JpaSpecificationExecutor<Perm> {

	@Query(value="SELECT * FROM perm WHERE perm_id NOT IN (SELECT perm_id FROM perm_permteam WHERE permteam_id = ?1)",nativeQuery = true)
	List<Perm> getPermallList(String permteamId);
	
	@Query(value="select * from perm where perm_id=?1",nativeQuery = true)
	List<Perm> getPerm(long permId);
	
	@Query(value="select t from Perm t where t.type= ?1")
	List<Perm> findAllMapByType(String type);

	@Query(value="select t from Perm t where t.totalModuleName= ?1 and t.isBlank = ?2 and t.noValidate = ?3 ")
	List<Perm> getPermsByTwoLevelId(String twoLevelId,String isBLank,String noValidate);

	@Query(value="select t from Perm t where t.permName = ?1 and t.requestMethod = ?2 ")
	Perm findByNameAndMethod(String requestName, String requestMethod);
	
	
	@Transactional
	 @Modifying
	 @Query(value = "SELECT p FROM Perm p " + 
	 		"	, AccountteamPermteam apteam" + 
	 		"	, Permteam pteam " + 
	 		"	, PermPermteam ppteam " + 
	 		"	, Accountteam ateam "
	 		+ " where  apteam.accountteamPermteamId.accountteamId = ?1 "
	 		+ " and pteam.permteamId =  apteam.accountteamPermteamId.permteamId and apteam.authType = ?3"
	 		+ " and ppteam.permpermteamId.permteamId = pteam.permteamId "
	 		+ " and p.permId = ppteam.permpermteamId.permId "
	 		+ " and p.type = ?2")
	List<Perm> getPermsByAccountteamId(long accountteamId, String type,String authType);
	
	
}
